﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using DAO;

namespace QLDaiLy
{
    public partial class NhapKho :XtraUserControl
    {
        public NhapKho()
        {
            InitializeComponent();
            LoadDanhSachMatHang();
            LoadDanhSachDonViTinh();
            //loadSoLuong();

        }
        private void LoadDanhSachMatHang()
        {
            List<MatHangDTO> list = new List<MatHangDTO>();
            list = MatHangDAO.LoadDanhSachMatHang();
            lookupMatHang.Properties.DataSource = list;
            lookupMatHang.Properties.ValueMember = "MaMatHang";
            lookupMatHang.Properties.DisplayMember = "TenMatHang";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;
            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenMatHang");
            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupMatHang.Properties.Columns.Add(col);
        }
        private void LoadDanhSachDonViTinh()
        {
            List<DonViTinhDTO> list = new List<DonViTinhDTO>();
            list = DonViTinhDAO.LoadDanhSachDonVi();
            lookupDonViTinh.Properties.DataSource = list;
            lookupDonViTinh.Properties.ValueMember = "MaDonVi";
            lookupDonViTinh.Properties.DisplayMember = "TenDonVi";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo colTenLoaiDaiLy;
            colTenLoaiDaiLy = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenDonVi");
            colTenLoaiDaiLy.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupDonViTinh.Properties.Columns.Add(colTenLoaiDaiLy);
        }
        private void btnCapNhatSL_Click(object sender, EventArgs e)
        {
            int tongsl = int.Parse(txtSoLuong.Text) +int.Parse(txtSoLuongTon.Text);
            bool kqs = GiaMatHangDAO.CapNhatSoLuong(int.Parse(lookupMatHang.EditValue.ToString()), int.Parse(lookupDonViTinh.EditValue.ToString()),tongsl);
            if (kqs == false)
            {
                MessageBox.Show("Không thể nhập kho");
            }
            else
            {
                txtSoLuong.Text = "";
            }
        }
        private void loadSoLuong()
        {
            int MaDonViTinh = lookupDonViTinh.EditValue.GetHashCode();
            int MaMatHang = lookupMatHang.EditValue.GetHashCode();
            DataTable dl = GiaMatHangDAO.GiaMatHang(MaDonViTinh, MaMatHang);
            DataRow dr = dl.Rows[0];
            txtSoLuongTon.Text = dr["SoLuongTon"].ToString();
        }
        private void lookupDonViTinh_EditValueChanged(object sender, EventArgs e)
        {
            loadSoLuong();
        }

        private void lookupMatHang_EditValueChanged(object sender, EventArgs e)
        {
            loadSoLuong();
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
